Rank N Polymorphism
ランクN多相、Arbitrary Rank Polymorphism
とある、関数の多相性
rank-1 typeとrank-2 typeの違い
参考
続くといいな日記 – 多相関数を第一級で取り扱う
Rank N Types in Haskell
#WIP
特に関数の型のことを指してRank-N Typesと言ったりする
以下も殆ど同じ使われ方をする
Rank-N Types
Arbitrary Rank Types
簡潔に
forall aの係る位置の話をしている
高階関数であるかどうかはあんまり関係ない
Nが2以上なら関係あるかmrsekut.icon
rank-1
code:hs
rank1 :: forall a. (a -> Int) -> Int
forallが全体にかかっている
rank-2
code:hs
rank2 :: (forall a. a -> Int) -> Int
forallが引数の関数にのみかかっている
例えば、rank-1 typeの例にlengthがある
code:hs
length :: forall a. a -> Int
任意の型の配列を取って、その長さを返す
ここで、lengthのような関数を引数に取る高階関数を考える
これも上と同じrank-1 typeな関数
以下は型errorになる
code:hs
rank1' :: forall a. (a -> Int) -> Int
rank1' f1 = f1 1,2,3
rank-2 typeを使えばerrorにはならない
code:hs
rank2 :: (forall a. a -> Int) -> Int
rank2 f1 = f1 1,2,3
rank-0 typeなものは単相関数と呼び、
rankが1以上のものを多相関数と呼ぶ
Haskell標準では多相関数をfrist class objectにできない
rank2以上からできるようになる
rank-0 type
rank-1 type
rank-2 type
rank-3 type
rank-2 typeな関数をfirst class objectとして扱う関数
例 ref
code:hs
f3 :: ((forall a. a -> a) -> Int) -> Bool -> Bool
rank-n type
rank-(n-1) typeな関数をfirst class objectとして扱う関数
haskell wikiでは
a rank-n type is a function that has at least one rank-(n-1) argument but no arguments of any higher rank. ref
少なくとも1つのrank-(n-1) typeな関数を引数に持ち、それより大きいrankの引数を持たないもの
これが正解なのかは不明mrsekut.icon
「引数」と限定している?
下のrank typeを包含しない?
rank-1 typeの関数 $ \notinrank-3 typeの関数
#??
コレはどういう意味になる?
code:hs
rank2 :: (forall a. (a -> a)) -> a -> a
rank2 g x = g x
カッコ内のaと、括弧外のaって違うもの?
明示的にforallを付けたらどうなる?
これって絶対に呼び出せない関数になる?
code:hs
aa1 = rank2 rank1 2 -- error
aa2 = rank2 rank0 2 -- error
理論的にはSystem F
とか、二階型付きラムダ計算
とか
haskell98はHindley-Milner 型システムを元にしている
これは制限のあるSystem Fのようなもの
system fの型推論は決定不能なのでhaskell98では採用していない
https://chrisdone.com/posts/rankntypes/
https://jozefg.bitbucket.io/posts/2014-01-14-existential.html
https://ocharles.org.uk/guest-posts/2014-12-18-rank-n-types.html
https://blog.euphonictech.com/entry/2015/04/05/204129
https://qiita.com/YoshikuniJujo/items/4094b5fe5a7a33f3d1e6
https://en.wikipedia.org/wiki/Parametric_polymorphism
On Understanding Types,Data Abstraction, and Polymorphism
http://lucacardelli.name/Papers/OnUnderstanding.A4.pdf
ランク1多相
https://www.pllab.riec.tohoku.ac.jp/smlsharp/ja/?Foundations%2F020
Type Inference with Rank 1 Polymorphismfor Type-Directed Compilation of ML
chrome-extension://oemmndcbldboiebfnladdacbdfmadadm/http://www.pllab.riec.tohoku.ac.jp/~ohori/research/icfp99.pdf
ランク2多相
https://qiita.com/YoshikuniJujo/items/c28d8fa11e33ed677e83
https://xtech.nikkei.com/it/article/COLUMN/20080603/305833/?ST=ittrend&P=2
https://ja.wikibooks.org/wiki/Haskell/Polymorphism
http://www.kotha.net/ghcguide_ja/latest/other-type-extensions.html#implicit-quant
https://ryota-ka.hatenablog.com/entry/2020/10/21/110000?utm_source=feed
https://qiita.com/mod_poppo/items/50ad2c0ee66171cc1ee9
ランクN多相
/LugendrePublic/任意ランクの多相
http://msakai.jp/d/20080529.html